package com.ccys.cms.service.consultation.impl;

import com.ccys.cms.service.consultation.PDFService;
import com.ccys.common.third.aws.s3.S3Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;

/**
 * @Author：XLY
 * @Date：2024/2/23 0023
 */
@Service
@Slf4j
public class PDFServiceImpl implements PDFService {


    @Override
    public void downloadPDF(HttpServletResponse response, String filename, String directory) {
        try {
            byte[] data = S3Util.getObjectBytes("", directory + filename);
            if(Objects.nonNull(data)){
                // 设置响应头信息
                response.setContentType("application/octet-stream");
                response.setContentLength(data.length);
                response.setHeader("Content-Disposition", "attachment; filename=" + filename);
                // 获取输出流
                ServletOutputStream  outputStream = response.getOutputStream();
                // 将数据写入输出流
                outputStream.write(data);
                // 刷新输出流
                outputStream.flush();
                outputStream.close();
            }

        } catch (IOException e) {
            log.error("下载PDF失败: "+e.getMessage());
        }
    }
}
